package me.midday.java.base.knowledge.reflection;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理的作用....
 */

interface Person{
    void walk();
    void sayHello(String name);
}

class Man implements Person{

    public void walk() {
        System.out.println("Man walk ....");
    }

    public void sayHello(String name) {
        System.out.println("Man say : " + name);
    }
}

class MyInvokationHandler implements InvocationHandler{

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("-----正在执行的方法：" + method);
        if (args!=null){
            System.out.println("下面是执行该方法时传入的实参：");
            for(Object val: args){
                System.out.println(val);
            }
        }else{
            System.out.println("没有传入参数");
        }
        return null;
    }
}

public class ProxyTest {
    public static void main(String[] args){
        InvocationHandler handler = new MyInvokationHandler();
        Person p = (Person) Proxy.newProxyInstance(Person.class.getClassLoader(), new Class[]{Person.class}, handler);
        p.walk();
        p.sayHello("hello");
    }
}
